/*
 * ParamArrayTest.java
 *
 * Copyright 2007 Christoph Csallner and Yannis Smaragdakis.
 */
package edu.gatech.cc.cnc.params;

import java.lang.reflect.InvocationTargetException;

import junit.framework.TestCase;
import client.Cut;
import client.CutReflect;
import edu.gatech.cc.cnc.firstpass.sym;
import edu.gatech.cc.jcrasher.plans.stmt.BlockStatement;

/**
 * @author csallner@gatech.edu (Christoph Csallner)
 */
public class ParamArrayTest extends TestCase {

  protected final CutReflect cut = new CutReflect(); 
  
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    
    ParamInt.resetSolver();
    ParamRefType.resetEq();
    Param.resetNames(cut.array(), Cut.class);
  }

  public void testDoubleArray()
  throws InvocationTargetException, InstantiationException, IllegalAccessException {
    ParamArray<double[]> x = new ParamArray<double[]>(double[].class, "x");
    ParamInt length = x.getArrayLength();
    
    length.constrain(sym.EQ, "5");

    ParamInt.prepareSolution(new ParamInt[]{length});
    ParamInt.generateSolution();
    
    BlockStatement<? extends double[]> plan = x.getPlan();
    //System.out.println(plan.text());
    double[] result = plan.execute();
    assertEquals(5, result.length);
  } 
  
  
  
  public void testIntArray()
  throws InvocationTargetException, InstantiationException, IllegalAccessException {
    ParamArray<int[]> x = new ParamArray<int[]>(int[].class, "x");
    ParamInt length = x.getArrayLength();
    
    length.constrain(sym.EQ, "7");

    ParamInt.prepareSolution(new ParamInt[]{length});
    ParamInt.generateSolution();
    
    BlockStatement<? extends int[]> plan = x.getPlan();
    //System.out.println(plan.text());
    int[] result = plan.execute();
    assertEquals(7, result.length);
  }
 
  
  public void testOversizedArray()
  throws InvocationTargetException, InstantiationException, IllegalAccessException {
    ParamArray<float[]> x = new ParamArray<float[]>(float[].class, "x");
    ParamInt length = x.getArrayLength();
    
    length.constrain(sym.GEQ, "100");

    ParamInt.prepareSolution(new ParamInt[]{length});
    ParamInt.generateSolution();
    
    BlockStatement<? extends float[]> plan = x.getPlan();
    //System.out.println(plan.text());
    float[] result = plan.execute();
    assertEquals(0, result.length);
    assertEquals("float[] f2 = new float[]{};", plan.text());
  } 
  
  
  public void testAlias()
  throws InvocationTargetException, InstantiationException, IllegalAccessException {
    ParamArray<byte[]> x = new ParamArray<byte[]>(byte[].class, "x");
    ParamArray<byte[]> y = new ParamArray<byte[]>(byte[].class, "y");
    ParamInt length = y.getArrayLength();
    
    length.constrain(sym.EQ, "2");
    x.constrain(sym.EQ, y);

    ParamInt.prepareSolution(new ParamInt[]{length});
    ParamInt.generateSolution();
    
    BlockStatement<? extends byte[]> xPlan = x.getPlan();
    BlockStatement<? extends byte[]> yPlan = y.getPlan();
    System.out.println(xPlan.text());
    System.out.println(yPlan.text());
    byte[] xResult = xPlan.execute();
    byte[] yResult = yPlan.execute();
    assertEquals(2, xResult.length);
    assertEquals(2, yResult.length);
    assertEquals(xResult, yResult);
    //assertEquals("byte[] f2 = new byte[]{};", plan.text());    
  }
}
